library(ggplot2)
library(patchwork)

# Study Models ------------------------------------------------------------
setwd("./Code/")
source("./main/study2_sems.R")
source("./main/study3_sems.R")
source("./main/study4_sems.R")


# Combine Studies ---------------------------------------------------------
ests_wide <- rbind(ests_wide_s2,
                   ests_wide_s3,
                   ests_wide_s4)

ests_wide$study <- factor(ests_wide$study,
                          labels = c("Study 2 (Dynata)", "Study 3 (MTurk)", "Study 4 (Bovitz)"))

# Compare to Naive Corrs --------------------------------------------------
obvar_corrs <- read.csv("../Data/naive_corr_labeled.csv")

obvar_corrs$alignrev <- factor(obvar_corrs$alignrev,
                               levels = c(0, 1),
                               labels = c("PW-PW", "NW-NW"))

obvar_corrs <- obvar_corrs[which(duplicated(obvar_corrs) == FALSE), ]

obvar_corrs$study <- paste("Study", obvar_corrs$study)

ests_wide$pair <- paste(ests_wide$lhs, ests_wide$rhs, sep = "-")
ests_wide$study2 <- NA
ests_wide$study2[which(ests_wide$study == "Study 2 (Dynata)")] <- "Study 2"
ests_wide$study2[which(ests_wide$study == "Study 3 (MTurk)")] <- "Study 3"
ests_wide$study2[which(ests_wide$study == "Study 4 (Bovitz)")] <- "Study 4"

corr_compare <- merge(ests_wide, obvar_corrs, by.x = c("study2", "pair"), by.y = c("study", "pair"))

corr_compare$corr_diff <- corr_compare$est.std.Method-corr_compare$cor1


# combine to plotting DF
abs_corr_diff <- data.frame(diff = c(mean(abs(corr_compare$corr_diff[which(corr_compare$alignrev == "PW-PW" & corr_compare$varcombstr %in% grep("Anti-Dem", corr_compare$varcombstr, value = T))])),
                                     mean(abs(corr_compare$corr_diff[which(corr_compare$alignrev == "PW-PW" & corr_compare$varcombstr %in% grep("Conspiracy", corr_compare$varcombstr, value = T))])),
                                     mean(abs(corr_compare$corr_diff[which(corr_compare$alignrev == "PW-PW" & corr_compare$varcombstr %in% grep("Chaos", corr_compare$varcombstr, value = T))])),
                                     mean(abs(corr_compare$corr_diff[which(corr_compare$alignrev == "PW-PW" & corr_compare$varcombstr %in% grep("Populism", corr_compare$varcombstr, value = T))])),
                                     mean(abs(corr_compare$corr_diff[which(corr_compare$alignrev == "PW-PW" & corr_compare$varcombstr %in% grep("Violence", corr_compare$varcombstr, value = T))])),
                                     mean(abs(corr_compare$corr_diff[which(corr_compare$alignrev == "PW-PW" & corr_compare$varcombstr %in% grep("Racism", corr_compare$varcombstr, value = T))])),
                                     mean(abs(corr_compare$corr_diff[which(corr_compare$alignrev == "PW-PW" & corr_compare$varcombstr %in% grep("Sexism", corr_compare$varcombstr, value = T))])),
                                     mean(abs(corr_compare$corr_diff[which(corr_compare$alignrev == "NW-NW" & corr_compare$varcombstr %in% grep("Anti-Dem", corr_compare$varcombstr, value = T))])),
                                     mean(abs(corr_compare$corr_diff[which(corr_compare$alignrev == "NW-NW" & corr_compare$varcombstr %in% grep("Conspiracy", corr_compare$varcombstr, value = T))])),
                                     mean(abs(corr_compare$corr_diff[which(corr_compare$alignrev == "NW-NW" & corr_compare$varcombstr %in% grep("Chaos", corr_compare$varcombstr, value = T))])),
                                     mean(abs(corr_compare$corr_diff[which(corr_compare$alignrev == "NW-NW" & corr_compare$varcombstr %in% grep("Populism", corr_compare$varcombstr, value = T))])),
                                     mean(abs(corr_compare$corr_diff[which(corr_compare$alignrev == "NW-NW" & corr_compare$varcombstr %in% grep("Violence", corr_compare$varcombstr, value = T))])),
                                     mean(abs(corr_compare$corr_diff[which(corr_compare$alignrev == "NW-NW" & corr_compare$varcombstr %in% grep("Racism", corr_compare$varcombstr, value = T))])),
                                     mean(abs(corr_compare$corr_diff[which(corr_compare$alignrev == "NW-NW" & corr_compare$varcombstr %in% grep("Sexism", corr_compare$varcombstr, value = T))]))),
                            var = rep(c("Anti-Dem", "Conspiracy", "Chaos", "Populism", "Violence", "Racism", "Sexism"),2),
                            alignrev = c(rep("PW-PW", 7), rep("NW-NW", 7)))


# Plot --------------------------------------------------------------------

v <- "Anti-Dem"
p_antidem <- ggplot(data = subset(corr_compare, 
                                  varcombstr %in% grep(v, corr_compare$varcombstr, value = T)), 
                    aes(x = cor1, y = est.std.Method)) +
  geom_abline(slope = 1, intercept = 0) +
  geom_point(size = 2, aes(shape = study)) +
  geom_text(data = subset(abs_corr_diff, var == v),
            aes(x = .55, y = -.1, 
                label = paste("|Diff.| =", round(diff, 2))),
            size = 3) +
  geom_smooth(color = "navy", se = FALSE, linetype = "dashed", method = "lm") +
  theme_bw() +
  facet_grid(~alignrev) +
  scale_shape_manual(values = c(16, 15)) +
  labs(title = "Anti-Democratic Attitudes",
       x = "Correlation using Observed Index", 
       y = "Correlation using SEM and Method Factor") +
  theme(legend.position = "bottom",
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.title = element_text(size = 12),
        axis.text = element_text(size = 12),
        legend.title = element_blank(),
        legend.text = element_text(size = 12),
        strip.text = element_text(size = 12),
        strip.background = element_blank())

v <- "Conspiracy"
p_consp <- ggplot(data = subset(corr_compare, 
                                varcombstr %in% grep(v, corr_compare$varcombstr, value = T)), 
                  aes(x = cor1, y = est.std.Method)) +
  geom_abline(slope = 1, intercept = 0) +
  geom_point(size = 2, aes(shape = study)) +
  geom_text(data = subset(abs_corr_diff, var == v),
            aes(x = .55, y = -.1, 
                label = paste("|Diff.| =", round(diff, 2))),
            size = 3) +
  geom_smooth(color = "navy", se = FALSE, linetype = "dashed", method = "lm") +
  theme_bw() +
  facet_grid(~alignrev) +
  scale_shape_manual(values = c(16, 17, 15)) +
  labs(title = "Conspiratorial Predispositions",
       x = "Correlation using Observed Index", 
       y = "Correlation using SEM and Method Factor") +
  theme(legend.position = "bottom",
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.title = element_text(size = 12),
        axis.text = element_text(size = 12),
        legend.title = element_blank(),
        legend.text = element_text(size = 12),
        strip.text = element_text(size = 12),
        strip.background = element_blank())

v <- "Chaos"
p_chaos <- ggplot(data = subset(corr_compare, 
                                varcombstr %in% grep(v, corr_compare$varcombstr, value = T)), 
                  aes(x = cor1, y = est.std.Method)) +
  geom_abline(slope = 1, intercept = 0) +
  geom_point(size = 2, aes(shape = study)) +
  geom_text(data = subset(abs_corr_diff, var == v),
            aes(x = .55, y = -.1, 
                label = paste("|Diff.| =", round(diff, 2))),
            size = 3) +
  geom_smooth(color = "navy", se = FALSE, linetype = "dashed", method = "lm") +
  theme_bw() +
  facet_grid(~alignrev) +
  scale_shape_manual(values = c(17, 15)) +
  labs(title = "Need for Chaos",
       x = "Correlation using Observed Index", 
       y = "Correlation using SEM and Method Factor") +
  theme(legend.position = "bottom",
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.title = element_text(size = 12),
        axis.text = element_text(size = 12),
        legend.title = element_blank(),
        legend.text = element_text(size = 12),
        strip.text = element_text(size = 12),
        strip.background = element_blank())

v <- "Populism"
p_pop <- ggplot(data = subset(corr_compare, 
                              varcombstr %in% grep(v, corr_compare$varcombstr, value = T)), 
                aes(x = cor1, y = est.std.Method)) +
  geom_abline(slope = 1, intercept = 0) +
  geom_point(size = 2, aes(shape = study)) +
  geom_text(data = subset(abs_corr_diff, var == v),
            aes(x = .55, y = -.1, 
                label = paste("|Diff.| =", round(diff, 2))),
            size = 3) +
  geom_smooth(color = "navy", se = FALSE, linetype = "dashed", method = "lm") +
  theme_bw() +
  facet_grid(~alignrev) +
  scale_shape_manual(values = c(16, 17, 15)) +
  labs(title = v,
       x = "Correlation using Observed Index", 
       y = "Correlation using SEM and Method Factor") +
  theme(legend.position = "bottom",
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.title = element_text(size = 12),
        axis.text = element_text(size = 12),
        legend.title = element_blank(),
        legend.text = element_text(size = 12),
        strip.text = element_text(size = 12),
        strip.background = element_blank())

v <- "Racism"
p_rr <- ggplot(data = subset(corr_compare, 
                             varcombstr %in% grep(v, corr_compare$varcombstr, value = T)), 
               aes(x = cor1, y = est.std.Method)) +
  geom_abline(slope = 1, intercept = 0) +
  geom_point(size = 2, aes(shape = study)) +
  geom_text(data = subset(abs_corr_diff, var == v),
            aes(x = .55, y = -.1, 
                label = paste("|Diff.| =", round(diff, 2))),
            size = 3) +
  geom_smooth(color = "navy", se = FALSE, linetype = "dashed", method = "lm") +
  theme_bw() +
  facet_grid(~alignrev) +
  scale_shape_manual(values = c(17, 15)) +
  labs(title = "Racial Resentment",
       x = "Correlation using Observed Index", 
       y = "Correlation using SEM and Method Factor") +
  theme(legend.position = "bottom",
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.title = element_text(size = 12),
        axis.text = element_text(size = 12),
        legend.title = element_blank(),
        legend.text = element_text(size = 12),
        strip.text = element_text(size = 12),
        strip.background = element_blank())

v <- "Sexism"
p_hs <- ggplot(data = subset(corr_compare, 
                             varcombstr %in% grep(v, corr_compare$varcombstr, value = T)), 
               aes(x = cor1, y = est.std.Method)) +
  geom_abline(slope = 1, intercept = 0) +
  geom_point(size = 2, aes(shape = study)) +
  geom_text(data = subset(abs_corr_diff, var == v),
            aes(x = .55, y = -.1, 
                label = paste("|Diff.| =", round(diff, 2))),
            size = 3) +
  geom_smooth(color = "navy", se = FALSE, linetype = "dashed", method = "lm") +
  theme_bw() +
  facet_grid(~alignrev) +
  scale_shape_manual(values = c(16, 17, 15)) +
  labs(title = "Hostile Sexism",
       x = "Correlation using Observed Index", 
       y = "Correlation using SEM and Method Factor") +
  theme(legend.position = "bottom",
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.title = element_text(size = 12),
        axis.text = element_text(size = 12),
        legend.title = element_blank(),
        legend.text = element_text(size = 12),
        strip.text = element_text(size = 12),
        strip.background = element_blank())

v <- "Violence"
p_pv <- ggplot(data = subset(corr_compare, 
                             varcombstr %in% grep(v, corr_compare$varcombstr, value = T)), 
               aes(x = cor1, y = est.std.Method)) +
  geom_abline(slope = 1, intercept = 0) +
  geom_point(size = 2, aes(shape = study)) +
  geom_text(data = subset(abs_corr_diff, var == v),
            aes(x = .55, y = -.1, 
                label = paste("|Diff.| =", round(diff, 2))),
            size = 3) +
  geom_smooth(color = "navy", se = FALSE, linetype = "dashed", method = "lm") +
  theme_bw() +
  facet_grid(~alignrev) +
  scale_shape_manual(values = c(17, 15)) +
  labs(title = "Political Violence",
       x = "Correlation using Observed Index", 
       y = "Correlation using SEM and Method Factor") +
  theme(legend.position = "bottom",
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.title = element_text(size = 12),
        axis.text = element_text(size = 12),
        legend.title = element_blank(),
        legend.text = element_text(size = 12),
        strip.text = element_text(size = 12),
        strip.background = element_blank())


(p_antidem + p_chaos + p_consp + p_hs + p_pv + p_pop + p_rr) &
  theme(axis.title = element_text(size = 10),
        axis.text = element_text(size = 10),
        legend.text = element_text(size = 10)) &
  labs(x = "Correlation using Observed Index",
       y = "Correlation using CFA\nand Method Factor") &
  scale_x_continuous(limits = c(-0.15, .80)) &
  scale_y_continuous(limits = c(-0.15, .80))
# ggsave("FigureA2.pdf", width = 10, height = 8)


# Construct Specific Variation --------------------------------------------
prop.table(table(subset(corr_compare, alignrev == "PW-PW" &
                          varcombstr %in% grep("Anti-Dem", corr_compare$varcombstr, value = T),
                        select = "corr_diff")>0))
prop.table(table(subset(corr_compare, alignrev == "PW-PW" &
                          varcombstr %in% grep("Conspiracy", corr_compare$varcombstr, value = T),
                        select = "corr_diff")>0))
prop.table(table(subset(corr_compare, alignrev == "PW-PW" &
                          varcombstr %in% grep("Chaos", corr_compare$varcombstr, value = T),
                        select = "corr_diff")>0))
prop.table(table(subset(corr_compare, alignrev == "PW-PW" &
                          varcombstr %in% grep("Populism", corr_compare$varcombstr, value = T),
                        select = "corr_diff")>0))
prop.table(table(subset(corr_compare, alignrev == "PW-PW" &
                          varcombstr %in% grep("Violence", corr_compare$varcombstr, value = T),
                        select = "corr_diff")>0))
prop.table(table(subset(corr_compare, alignrev == "PW-PW" &
                          varcombstr %in% grep("Racism", corr_compare$varcombstr, value = T),
                        select = "corr_diff")>0))
prop.table(table(subset(corr_compare, alignrev == "PW-PW" &
                          varcombstr %in% grep("Sexism", corr_compare$varcombstr, value = T),
                        select = "corr_diff")>0))

prop.table(table(subset(corr_compare, alignrev == "NW-NW" &
                          varcombstr %in% grep("Anti-Dem", corr_compare$varcombstr, value = T),
                        select = "corr_diff")>0))
prop.table(table(subset(corr_compare, alignrev == "NW-NW" &
                          varcombstr %in% grep("Conspiracy", corr_compare$varcombstr, value = T),
                        select = "corr_diff")>0))
prop.table(table(subset(corr_compare, alignrev == "NW-NW" &
                          varcombstr %in% grep("Chaos", corr_compare$varcombstr, value = T),
                        select = "corr_diff")>0))
prop.table(table(subset(corr_compare, alignrev == "NW-NW" &
                          varcombstr %in% grep("Populism", corr_compare$varcombstr, value = T),
                        select = "corr_diff")>0))
prop.table(table(subset(corr_compare, alignrev == "NW-NW" &
                          varcombstr %in% grep("Violence", corr_compare$varcombstr, value = T),
                        select = "corr_diff")>0))
prop.table(table(subset(corr_compare, alignrev == "NW-NW" &
                          varcombstr %in% grep("Racism", corr_compare$varcombstr, value = T),
                        select = "corr_diff")>0))
prop.table(table(subset(corr_compare, alignrev == "NW-NW" &
                          varcombstr %in% grep("Sexism", corr_compare$varcombstr, value = T),
                        select = "corr_diff")>0))